Fix createFromFormat to respect setTestNow for missing components#494
Fix createFromFormat to respect setTestNow for missing components#494dereuromark merged 1 commit into3.xfrom
Conversation
When a test time is set via setTestNow(), createFromFormat() now uses values from the test time for any date/time components not present in the format string. This makes testing code that uses partial date formats more reliable and predictable. The fix: - Detects which date/time components are in the format string - For missing components, uses values from testNow instead of real time - Respects '\!' and '|' modifiers which intentionally reset components Closes #452
| // If the format includes '!' or '|', PHP resets unspecified components to Unix epoch or zero | ||
| // In that case, we should not override with testNow | ||
| $hasReset = in_array('!', $formatChars, true) || in_array('|', $formatChars, true); | ||
| if ($hasReset) { | ||
| return $dateTime; | ||
| } |
There was a problem hiding this comment.
Why are we adding new syntax to PHP's datetime formats?
There was a problem hiding this comment.
We're not adding new syntax - ! and | are existing PHP DateTime format modifiers (see PHP docs).
The code is detecting their presence so we know to skip applying testNow values. When a user explicitly uses ! or |, they're telling PHP to reset unspecified components to Unix epoch, so we should respect that intent rather than substituting testNow values.
There was a problem hiding this comment.
LOL.. never knew such modifiers existed.
There was a problem hiding this comment.
Open for alternative solutions to solving this issue, though :)
There was a problem hiding this comment.
LOL.. never knew such modifiers existed.
Same. Carry on then.
Summary
When
setTestNow()is used for testing,createFromFormat()now uses values from the test time for any date/time components not present in the format string.Previously, partial formats like
createFromFormat('m-d', '10-05')would use the real system time for the year, making tests unreliable. Now it uses the testNow year.The fix:
!and|modifiers which intentionally reset unspecified components to Unix epoch/zeroExample
Closes #452